#include <iostream>
#include <string>
#include <vector>
using namespace std;
//备忘录模式：备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。
//备忘录模式的用意是在不破坏封装的条件下，将一个对象的状态捉住，
//并外部化，存储起来，从而可以在将来合适的时候把这个对象还原到存储起来的状态。


//备忘录的节点
class Memo
{
public:
	//记录当前的状态
	string state;
	Memo(string state)
	{
		this->state = state;
	}
};

//类的包含备忘录的节点
class Originator
{
public:
	string state;
	void setMemo(Memo *memo)
	{
		this->state = memo->state;
	}

	Memo *createMemo()
	{
		return new Memo(state);
	}

	void show()
	{
		cout << state << endl;
	}
};

//备忘录的集合
class Caretaker
{
public:
	vector<Memo *>memos;
	void save(Memo *memo)
	{
		this->memos.push_back(memo);
	}

	Memo * getState(int i)
	{
		return memos[i];
	}
};

void main1()
{
	Originator *org = new Originator();
	Caretaker *ct = new Caretaker();
	//设置状态
	org->state = "on";
	//保存状态
	ct->save(org->createMemo());
	//获取状态
	Memo * memo = ct->getState(0);
	cout << memo->state << endl;

	//改变状态
	org->state = "off";
	//再次保存
	ct->save(org->createMemo());
	cout << ct->getState(1)->state << endl;
	delete(ct);
	delete(org);

	system("pause");
}